package com.songshuai.webapi.jobapi.util;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.util.DigestUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Util {

    //人事离职_字典code
    public static Integer personnelstatus_code = 713;
    //教师级别_字典code
    public static Integer Teacher_lv_senior_name_code = 233;

    /**
     * 添加数据源
     *
     * @param driverClass
     * @param url
     * @param user
     * @param password
     * @return
     */
    public static DruidDataSource setDataSource(String driverClass, String url, String user, String password) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }

    /**
     * MD5方法
     *
     * @param text 明文
     * @return 密文
     * @throws Exception
     */
    public static String md5(String text) {
        String retMd5="";
        if (text == null || text.length() == 0) {
            return "";
        }
        //加密后的字符串
        try{
            retMd5 = DigestUtils.md5DigestAsHex(text.getBytes("utf-8"));
        }catch (Exception ex){}
        return retMd5;
    }

    /**
     * 计算年龄
     *
     * @param date
     * @return
     */
    public static Integer calcAge(Date date) {
        if (date == null) {
            return 0;
        }
        Integer currYear = Calendar.getInstance().get(Calendar.YEAR);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Integer birthYear = calendar.get(Calendar.YEAR);
        return currYear - birthYear;
    }

    /**
     * 日期转字符串
     *
     * @param input
     * @return
     */
    public static String ChangeDateToString(Date input, String format) {
        String retData = "";
        if (input == null) {
            return retData;
        }
        if (format == null) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(input);
    }

    /**
     * 字符串转日期
     *
     * @param input
     * @return
     */
    public static Date ChangeStringToDate(String input, String format) {
        if (IsNullAndEmpty(input)) {
            return null;
        }

        if (format == null) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

        try {
            return simpleDateFormat.parse(input);
        } catch (Exception ex) {

        }
        return null;
    }

    /**
     * 判断字符串
     *
     * @param str
     * @return
     */
    public static boolean IsNullAndEmpty(String str) {
        boolean response = true;
        if (str != null && str.length() > 0) {
            response = false;
        }
        return response;
    }

    /**
     * 获取老签到状态
     *
     * @param request
     * @return
     */
    public static Integer get_old_qian_dao(Integer request) {
        Integer response = 0;
        if (request == null) {
            return response;
        }
        //0：未设置 1：到 2：未到  5:顺延 6:调课
        //492:正常上课 493:学生迟到 494: 学生旷课 495: 提前下课 496:教师未上课   497:课程异常 498:学生调课 499:教师忘记填写 500:学生未上课
        switch (request) {
            case 492:
                response = 1;
                break;
            case 494:
            case 496:
            case 497:
            case 500:
                response = 2;
                break;
            case 498:
                response = 6;
                break;
            case 499:
                response = 0;
                break;
        }
        return response;
    }

    /**
     * 获取对象的字符串值
     *
     * @param input
     * @param defualtValue
     * @return
     */
    public static String getStringValue(Object input, String defualtValue) {
        String retDefault = defualtValue;
        if (retDefault == null) {
            retDefault = "";
        }
        if (input == null) {
            return retDefault;
        }
        return input.toString();
    }

    /**
     * 重载tostring
     * @param input
     * @return
     */
    public static String getStringValue(Object input) {
        return input == null ? "" : input.toString();
    }

    /**
     * 获取默认值int
     * @param i
     * @param defaultValue
     * @return
     */
    public static Integer getDefaultValueInt(Integer i, Integer defaultValue) {
        Integer resDefault = 0;
        if (defaultValue != null) {
            resDefault = defaultValue;
        }

        return i == null ? resDefault : i;
    }

    /**
     * 重载获取默认值int
     * @param i
     * @return
     */
    public static Integer getDefaultValueInt(Integer i) {
        return i == null ? 0 : i;
    }

    /**
     * 获取默认值long
     * @param i
     * @param defaultValue
     * @return
     */
    public static Long getDefaultValueLong(Long l, Long defaultValue) {
        Long resDefault = 0L;
        if (defaultValue != null) {
            resDefault = defaultValue;
        }

        return l == null ? resDefault : l;
    }

    /**
     * 重载获取默认值long
     * @param i
     * @return
     */
    public static Long getDefaultValueLong(Long i) {
        return i == null ? 0 : i;
    }

    /**
     * 取得当前时间戳（精确到秒）
     * @return
     */
    public static String timeStamp(){
        long time = System.currentTimeMillis();
        String t = String.valueOf(time/1000);
        return t;
    }
}
